<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<LINK type="text/css" rel="stylesheet" href="${ctx}/css/style.css"/>
<script type="text/javascript" src="${ctx}/js/xtable.js"></script>
<script type="text/javascript" src="${ctx}/js/xtree.js"></script>
<script type="text/javascript" src="${ctx}/js/validator.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/SecurityService.js"></script>
<script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body onload="init()">
<table style="width:1000px">
    <tr>
        <td style="vertical-align:top">
            <SCRIPT language="javascript" type="">
            var resourceTree = new WebFXTree("0", "资源", "javascript:setResource(null)");
            </SCRIPT>
        </td>
        <td align="center" style="vertical-align:top">
            <form id="resourceForm" name="resourceForm">
            <table style="width:500px">
                <tr>
                    <td>
                        资源名称
                    </td>
                    <td>
                        <input type="text" id="resourceName" name="resourceName" style="width:300px" elname="资源名称" required="1" maxLen="100"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        资源地址
                    </td>
                    <td>
                        <input type="text" id="resourceUrl" name="resourceUrl" style="width:300px" elname="资源地址" maxLen="200"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        资源顺序
                    </td>
                    <td>
                        <input type="text" id="resourceSeq" name="resourceSeq" style="width:300px" elname="资源顺序" required="1" />
                    </td>
                </tr>
                <tr>
                    <td>
                        资源图标
                    </td>
                    <td>
                        <input type="text" id="resourceIcon" name="resourceIcon" style="width:300px" elname="资源备注" />
                    </td>
                </tr>
                <tr>
                    <td>
                        资源类型
                    </td>
                    <td>
                        <select id="resourceType" name="resourceType" elname="资源类型">
                            <option value="1">菜单</option>
                            <option value="2">按钮</option>
                            <option value="3">url</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        是否有效
                    </td>
                    <td>
                        <select id="resourceState" name="resourceState" elname="是否有效">
                            <option value="1">有效</option>
                            <option value="2">无效</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        资源备注
                    </td>
                    <td>
                        <input type="text" id="resourceRemark" name="resourceRemark" style="width:300px" elname="资源地址" maxLen="200"/>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="hidden" id="resourceId" name="resourceId" />
                        <input type="hidden" id="applicationId" name="applicationId" />
                        <img src="${ctx}/images/common/button_insert.jpg" title="添加资源" style="cursor:pointer" onclick="insertResource()"/>
                        <img src="${ctx}/images/common/button_update.jpg" title="修改资源" style="cursor:pointer" onclick="updateResource()"/>
                        <img src="${ctx}/images/common/button_delete.jpg" title="删除资源" style="cursor:pointer" onclick="deleteResource()"/>
                    </td>
                </tr>
            </table>
            </form>
        </td>
    </tr>
</table>
</body>
</html>
<script type="text/javascript">
function init(){
    var application = new Application();
    application.pageIndex = 1;
    application.pageSize = 100;
    SecurityService.selectApplicationResourceList(application, callbackSelectResourceList);
}
//解析返回的结果
function callbackSelectResourceList(list){
    var application;
    var resourceList;
    for(var i = 0; i < list.length; i++){
        application = list[i];
        var node = new WebFXTreeItem(application.applicationId, application.applicationName,  "javascript:setApplication('" + application.applicationId + "')", null, resourceTree);
        resourceList = application.resourceList;
        addResourceNode(resourceList, node);
    }
    resourceTree.expandAll();
}

function addResourceNode(list, parentNode){
    for(var i = 0; i < list.length; i++){
        var resource = list[i];
//        var node = addResourceNode(resource, parentNode);
        var node = new WebFXTreeItem(resource.resourceId, resource.resourceName, "javascript:setResource('" + resource.resourceId + "')", parentNode, resourceTree);
        var resourceList = resource.resourceList;
        if(resourceList != null && resourceList.length > 0){
            addResourceNode(resourceList, node);
        }
//        addResourceNode(resourceList, node);
    }
}

//function addResourceNode(resource, parentNode){
//    var node = new WebFXTreeItem(resource.resourceId, resource.resourceName, "javascript:setResource('" + resource.resourceId + "')", parentNode, resourceTree);
//    return node;
//}

function setApplication(applicationId) {
    dwr.util.setValue("applicationId", applicationId);
    dwr.util.setValue("resourceId", "");
    dwr.util.setValue("resourceName", "");
    dwr.util.setValue("resourceUrl", "");
    dwr.util.setValue("resourceSeq", "");
    dwr.util.setValue("resourceIcon", "");
    dwr.util.setValue("resourceType", "");
    dwr.util.setValue("resourceRemark", "");
    dwr.util.setValue("resourceState", "");
}

function setResource(resourceId){
    if (resourceId == null) {
        $("resourceForm").reset();
    } else {
        var resource = new Resource();
        resource.resourceId = resourceId;
        SecurityService.selectResourceList(resource, function (list) {
            resource = list[0];
            dwr.util.setValue("resourceId", resource.resourceId);
            dwr.util.setValue("resourceName", resource.resourceName);
            dwr.util.setValue("resourceUrl", resource.resourceUrl ? resource.resourceUrl : "");
            dwr.util.setValue("resourceSeq", resource.resourceSeq);
            dwr.util.setValue("resourceIcon", resource.resourceIcon ? resource.resourceIcon : "");
            dwr.util.setValue("resourceType", resource.resourceType);
            dwr.util.setValue("resourceRemark", resource.resourceRemark ? resource.resourceRemark : "");
            dwr.util.setValue("resourceState", resource.resourceState);
            dwr.util.setValue("applicationId", resource.applicationId);
        });
    }
}

function insertResource(){
    var _form = $("resourceForm");
    if(!validate(_form)){
        return false;
    }

    var resourceId = dwr.util.getValue("resourceId");

    var resource = new Resource();
    if(resourceId != ""){
        resource.resourceParentId = resourceId;
    }
    else{
        resource.resourceParentId = 1;
    }
    resource.resourceName = dwr.util.getValue("resourceName");
    resource.resourceUrl = dwr.util.getValue("resourceUrl");
    resource.resourceSeq = dwr.util.getValue("resourceSeq");
    resource.resourceIcon = dwr.util.getValue("resourceIcon");
    resource.resourceType = dwr.util.getValue("resourceType");
    resource.resourceRemark = dwr.util.getValue("resourceRemark");
    resource.resourceState = dwr.util.getValue("resourceState");
    resource.applicationId = dwr.util.getValue("applicationId");
    SecurityService.insertResource(resource, callbackInsertResource);
}

function callbackInsertResource(resource){
    var parentNode = resourceTree.getSelected();
    addResourceNode(resource, parentNode);
}

function updateResource(){
    var _form = $("resourceForm");
    if(!validate(_form)){
        return false;
    }

    if(dwr.util.getValue("resourceId") != ""){
        if(!confirm("你确定要更新此资源信息")){
            return false;
        }
        var resource = new Resource();
        resource.resourceId = dwr.util.getValue("resourceId");
        resource.resourceName = dwr.util.getValue("resourceName");
        resource.resourceUrl = dwr.util.getValue("resourceUrl");
        resource.resourceSeq = dwr.util.getValue("resourceSeq");
        resource.resourceIcon = dwr.util.getValue("resourceIcon");
        resource.resourceType = dwr.util.getValue("resourceType");
        resource.resourceRemark = dwr.util.getValue("resourceRemark");
        resource.resourceState = dwr.util.getValue("resourceState");
        resource.applicationId = dwr.util.getValue("applicationId");
        SecurityService.updateResource(resource, callbackResource);
    }
    else{
        alert("请选择要更新的资源");
    }
}

function deleteResource(){
    var resourceId = dwr.util.getValue("resourceId");
    if(!resourceId ){
        alert("请你选择要删除的资源")
        return false;
    }
    if(!confirm("你确定要删除此资源信息")){
        return false;
    }
    var resource = new Resource();
    resource.resourceId = resourceId;
    SecurityService.deleteResource(resource, callbackResource);
}

function callbackResource(){
    location.reload();
}

</script>
